import _ from "lodash";
import { message } from "antd";
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { EXCELDATA } from "../constants";

const { getLabel } = WeaLocaleProvider;
const { success, error } = message;
const { callApi } = WeaTools;

export default {
  api: modName => {
    return (action, settings = {}) => { // 封装callApi
      let {
        data = '',
        msg = '',
        type = 'get',
        dataType = 'json',
        ignoreError = false,
        url = `${EXCELDATA}${modName}/${action}`
      } = settings;
      return new Promise((resolve, reject) => {
        callApi(url, type, data, dataType).then(res => {
          if (res.status) {
            msg && success(msg);
            return resolve(res);
          }

          let errmsg = res.api_errormsg || getLabel(383311, "服务器处理请求时出现异常");

          errmsg = !ignoreError && errmsg.replace("catch exception :", "").trim();

          errmsg && error(errmsg)

          return reject(res);
        });
      });
    };
  },
  UUID: (len = 32) => {
    return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      const r = (Math.random() * 16) | 0;
      const v = c === 'x' ? r : ((r & 0x3) | 0x8);

      return v.toString(16);
    }).substring(0, len);
  },
  isUrl: str => /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig.test(str),
  arrayMove: (array, from, to) => {
    array = array.slice();
    array.splice((to < 0 ? array.length + to : to), 0, array.splice(from, 1)[0]);
    return array;
  }
};